package cn.mifengkong.appcommon.webview.jshandler;

import com.mifengkong.frtools.jsbridge.CallBackFunction;
import com.mifengkong.frtools.jsbridge.JsHandler;
import com.mifengkong.frtools.util.FRString;

import org.json.JSONException;
import org.json.JSONObject;

import cn.mifengkong.appcommon.webview.listener.JsBridgeCallBackListener;

/**
 * Created by jack on 2018/3/6
 */

public class AuthorizeServiceJsHandler implements JsHandler {

    private String code;
    private String productId;

    private JsBridgeCallBackListener mListener;

    public AuthorizeServiceJsHandler(JsBridgeCallBackListener listener) {
        mListener=listener;
    }

    @Override
    public void OnHandler(String handlerName, String responseData, CallBackFunction function) {
        if (null!=mListener && parseResponseData(responseData)){
            mListener.dataImport(code,productId);
        }
    }

    /**
     * 是否需要跳转支付页面
     */
    private boolean parseResponseData(String responseData) {
        try {
            JSONObject responseDataJO = new JSONObject(responseData);
            code = responseDataJO.getString("code");
            productId = responseDataJO.getString("product_id");
            return !FRString.isEmpty(code,productId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
}
